package com.ldy.core.util;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.legacy.math.linearalgebra.ByteUtils;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cn.hutool.core.io.IORuntimeException;

public class Sm4Utils {
    private static final String ENCODING = "UTF-8";
    public static final String ALGORITHM_NAME = "SM4";
    public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS5Padding";
    public static final int DEFAULT_KEY_SIZE = 128;

    public Sm4Utils() {
    }

    public static String encryptEcb(String hexKey, String paramStr) {
        String cipherText = "";

        try {
            byte[] keyData = ByteUtils.fromHexString(hexKey);
            byte[] srcData = paramStr.getBytes("UTF-8");
            byte[] cipherArray = encryptEcbPadding(keyData, srcData);
            cipherText = ByteUtils.toHexString(cipherArray);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return cipherText;
    }

    public static String decryptEcb(String hexKey, String cipherText) {
        String decryptStr = "";

        try {
            byte[] keyData = ByteUtils.fromHexString(hexKey);
            byte[] cipherData = ByteUtils.fromHexString(cipherText);
            byte[] srcData = decryptEcbPadding(keyData, cipherData);
            decryptStr = new String(srcData, "UTF-8");
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return decryptStr;
    }

    public static String generateSecretKey() throws Exception {
        return ByteUtils.toHexString(generateKey());
    }

    private static byte[] generateKey() throws Exception {
        KeyGenerator kg = KeyGenerator.getInstance("SM4", "BC");
        kg.init(128, new SecureRandom());
        return kg.generateKey().getEncoded();
    }

    private static byte[] encryptEcbPadding(byte[] key, byte[] data) throws Exception {
        Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 1, key);
        return cipher.doFinal(data);
    }

    private static byte[] decryptEcbPadding(byte[] key, byte[] cipherText) throws Exception {
        Cipher cipher = generateEcbCipher("SM4/ECB/PKCS5Padding", 2, key);
        return cipher.doFinal(cipherText);
    }

    private static Cipher generateEcbCipher(String algorithmName, int mode, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithmName, "BC");
        Key sm4Key = new SecretKeySpec(key, "SM4");
        cipher.init(mode, sm4Key);
        return cipher;
    }

    static {
        Security.addProvider(new BouncyCastleProvider());
    }
    
    private static String SM4_KEY = "faccd6ee47f915b3a9465d0e34acc0f0";
    private static Gson gson = new GsonBuilder()
    		.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
    		.disableHtmlEscaping()
    		.create();
    public static void main(String[] args) throws IORuntimeException, IOException {
    	String jsonFile = "data.json";
    	String name = Sm4Utils.class.getPackage().getName();
    	Path path = Paths.get("src/main/java", name.replace(".", "/"), jsonFile);
    	try (InputStream io = new FileInputStream(path.toFile())) {
    		ByteArrayOutputStream bos = new ByteArrayOutputStream();
        	org.apache.commons.io.IOUtils.copy(io, bos);
        	String json = new String(bos.toByteArray());
//        	System.out.println(json);
        	String encryptEcb = encryptEcb(SM4_KEY, json);
        	System.out.println(encryptEcb);
    	}catch (Exception e) {
    		e.printStackTrace();
		}
    
	}
}
